package com.training1.core.tree.misc;

public final class TreeLayoutDefinition{
	public final static int TREE_SIZE = 40;
	public final static int TREE_LEVEL_TOP= 1;
	public final static int TREE_LEVEL_FIRST = 2;
	public final static int TREE_LEVEL_SECOND = 3;
	public final static int TREE_LEVEL_THIRD = 4;
	public final static int TREE_LEVEL_INVALID = -100;
	public final static int TREE_NODE_INVALID = -10000;
	
	// node index 0 ~ 40 match view index 0 ~ 40
	private final static int[] NINDEX_2_VINDEX_LOOKUP_TABLE = new int[] {
			0, 
	     1, 2, 3, 
	4, 7, 10, 5, 8, 11, 6, 9, 12,
	13, 16, 19, 22, 25, 28, 31, 34, 37, 14, 17, 20, 23, 26, 29, 32, 35, 38, 15, 18, 21, 24, 27, 30, 33, 36, 39};
	
	// node index 0 ~ 40 match view index 0 ~ 40
	private final static int[] VINDEX_2_PINDEX_LOOKUP_TABLE = new int[] {
			-1, 
	     0, 0, 0, 
	1, 1, 1, 2, 2, 2, 3, 3, 3,
	4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 12};
	
	// base 0
	public static int getNodeCountInLevel(int level) {
		if(level == TREE_LEVEL_TOP) {
			return 1;
		}
		else if(level == TREE_LEVEL_FIRST) {
			return 3;
		}
		else if(level == TREE_LEVEL_SECOND) {
			return 9;
		}
		else if(level == TREE_LEVEL_THIRD) {
			return 27;
		}
		return TREE_LEVEL_INVALID;
	}
	
	public static int findViewIndex(int index) {
		return NINDEX_2_VINDEX_LOOKUP_TABLE[index];
	}
	
	public static int findParentViewIndex(int viewIndex) {
		return VINDEX_2_PINDEX_LOOKUP_TABLE[viewIndex];
	}
	/**
	 * return sub node view index
	 * @param viewIndex
	 * @return
	 */
	public static int[] findSubNodes(int viewIndex) {
		switch(viewIndex) {
		case 0:
			return new int[]{1, 2, 3};
		case 1:
			return new int[]{4,5,6};
		case 2:
			return new int[]{7,8,9};
		case 3:
			return new int[]{10,11,12};
		case 4:
			return new int[]{13,14,15};
		case 5:
			return new int[]{16, 17, 18};
		case 6:
			return new int[]{19,20,21};
		case 7:
			return new int[]{22,23,24};
		case 8:
			return new int[]{25,26,27};
		case 9:
			return new int[]{28,29,30};
		case 10:
			return new int[]{31,32,33};
		case 11:
			return new int[]{34,35,36};
		case 12:
			return new int[]{37,38,39};
		default:
			return new int[]{};
		}
	}
	
	// base 0
	public static int getLevelIndex(int viewIndex) {
		if(viewIndex == 0) {
			return TREE_LEVEL_TOP;
		}
		else if(viewIndex >= 1 && viewIndex <= 3) {
			return TREE_LEVEL_FIRST;
		}
		else if(viewIndex >= 4 && viewIndex <= 12) {
			return TREE_LEVEL_SECOND;
		}
		else  {
			return TREE_LEVEL_THIRD;
		}
	}

}
